package com.atguigu.gmall.mq.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

//  这个配置类起什么作用? -- 初始化绑定关系！
@Configuration
public class DeadLetterMqConfig {

    //  声明变量
    public static final String exchange_dead = "exchange.dead";
    public static final String routing_dead_1 = "routing.dead.1";
    public static final String routing_dead_2 = "routing.dead.2";
    public static final String queue_dead_1 = "queue.dead.1";
    public static final String queue_dead_2 = "queue.dead.2";

    //  创建一个交换机到spring容器
    @Bean
    public DirectExchange exchange(){
        return new DirectExchange(exchange_dead,true,false);
    }

    //  创建队列1
    @Bean
    public Queue queue(){
        //  设置ttl
        HashMap<String, Object> map = new HashMap<>();
        //  设置交换机---死信交换机
        map.put("x-dead-letter-exchange",exchange_dead);
        map.put("x-dead-letter-routing-key",routing_dead_2);
        //  设置消息的过期时间
        map.put("x-message-ttl",10000);
        return new Queue(queue_dead_1,true,false,false,map);
    }

    //  设置绑定关系！
    @Bean
    public Binding binding(){
        //  返回绑定关系！
        return BindingBuilder.bind(queue()).to(exchange()).with(routing_dead_1);
    }

    //  设置队列2
    @Bean
    public Queue queue2(){
        return new Queue(queue_dead_2,true,false,false);
    }

    //  设置绑定关系！
    @Bean
    public Binding binding2(){
        //  返回绑定关系！
        return BindingBuilder.bind(queue2()).to(exchange()).with(routing_dead_2);
    }

}
